package com.oriole.admin.controller;

import com.oriole.admin.feign.ISysUserService;
import com.oriole.admin.utils.PageableUtil;
import com.oriole.admin.utils.OriolePage;
import com.oriole.common.constant.ResultModel;
import com.oriole.entity.sys.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author doublelifeke
 * Email: hautxxxyzjk@163.com
 * DateTime: 2019/4/11 12:53
 * Description:
 */
@Api(tags = {"系统用户接口"})
@Controller
@RequestMapping("/sysUser")
public class SysUserController {

    @Resource
    private ISysUserService sysUserService;

    @ApiOperation(value = "保存用户")
    @ResponseBody
    @PostMapping("/save")
    @Secured("ROLE_SUPER")
    public ResultModel<SysUser> save(@Valid @RequestBody SysUser sysUser) {
        return sysUserService.save(sysUser);
    }

    @ApiOperation(value = "修改用户")
    @ResponseBody
    @PutMapping("/update")
    @Secured("ROLE_SUPER")
    public ResultModel<SysUser> update(@Valid @RequestBody SysUser sysUser) {
        return sysUserService.update(sysUser);
    }

    @ApiOperation(value = "删除用户")
    @ResponseBody
    @DeleteMapping("/delete/{id}")
    @Secured("ROLE_SUPER")
    public ResultModel<Object> delete(@PathVariable("id") Long id) {
        return sysUserService.deleteById(id);
    }

    @ApiOperation(value = "批量删除用户")
    @ResponseBody
    @DeleteMapping("/deleteBatchByIds")
    @Secured("ROLE_SUPER")
    public ResultModel<Object> deleteBatchByIds(@RequestParam("ids") String ids) {
        ArrayList<Long> longs = new ArrayList<>();
        for (String id: ids.split(",")) {
            longs.add(Long.parseLong(id));
        }
        return sysUserService.deleteBatchByIds(longs);
    }

    @ApiOperation(value = "根据ID查询用户")
    @ResponseBody
    @GetMapping("/find/{id}")
    @Secured("ROLE_SUPER")
    public ResultModel<SysUser> findById(@PathVariable("id") Long id) {
        return sysUserService.findById(id);
    }

    @ApiOperation(value = "获取系统用户列表")
    @ResponseBody
    @GetMapping("/findAll")
    @Secured("ROLE_SUPER")
    public ResultModel<List<SysUser>> findAll() {
        return sysUserService.findAll();
    }

    @ApiOperation(value = "获取系统用户列表分页")
    @ResponseBody
    @GetMapping("/findPage")
    @Secured("ROLE_SUPER")
    public ResultModel<Object> findPage(OriolePage page) {
        Pageable pageable = PageableUtil.getPageable(page);
        ResultModel<Object> resultModel = sysUserService.findPage(pageable);
        return ResultModel.success(PageableUtil.setResultPage(resultModel));
    }
}
